export default {
  namespaced: true,
  state: () => ({
    // 地址
    address: JSON.parse(uni.getStorageSync('address') || '{}'),
    // 用户信息
    userinfo: JSON.parse(uni.getStorageSync('userinfo') || '{}'),
    token: JSON.parse(uni.getStorageSync('token') || '""'),
    // 跳转信息存储
    switchData: {}
  }),
  mutations: {
    // 修改收获地址
    updateAddress(state, address) {
      state.address = address
      this.commit('m_user/saveAddressToStorage')
    },
    // 更新用户信息
    updateUserInfo(state, userinfo) {
      state.userinfo = userinfo
      this.commit('m_user/saveUserInfoToStorage')
    },
    // 修改token值
    updateToken(state, token) {
      state.token = token
      this.commit('m_user/saveTokenToStorage')
    },
    // 更新跳转信息
    updateSwitchData(state, info) {
      state.switchData = info
    },
    // 持久化保存地址
    saveAddressToStorage(state) {
      uni.setStorageSync('address', JSON.stringify(state.address))
    },
    // 持久化保存用户信息
    saveUserInfoToStorage(state) {
      uni.setStorageSync('userinfo', JSON.stringify(state.userinfo))
    },
    // 持久化保存token
    saveTokenToStorage(state) {
      uni.setStorageSync('token', JSON.stringify(state.token))
    },
  },
  getters: {
    // 收货地址拼接
    addressStr(state) {
      if (state.address.provinceName) {
        return state.address.provinceName + state.address.cityName + state.address.countyName + state.address.detailInfo
      } else {
        return ''
      }
    }
  }
}
